package com.ruoyi.web.controller.business;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Enterprise;
import com.ruoyi.system.service.IEnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * 企业Controller
 *
 * @author gzhuangwei
 * @date 2022-02-26
 */
@RestController
@RequestMapping("/business/enterprise")
public class EnterpriseController extends BaseController
{
    @Autowired
    private IEnterpriseService enterpriseService;

    /**
     * 查询企业列表
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise:list')")
    @GetMapping("/list")
    public TableDataInfo list(Enterprise enterprise)
    {
        startPage();
        // 得到帐户用户名和用户类型
        Long createId = SecurityUtils.getLoginUser().getUser().getUserId();
        String createUserType = SecurityUtils.getLoginUser().getUser().getUserType();
        if ( !createUserType.equals("00")) {
            enterprise.setEnteCreateId(createId);
        }
        // 获取列表
        List<Enterprise> list = enterpriseService.selectEnterpriseList(enterprise);
        return getDataTable(list);
    }

    /**
     * 导出企业列表
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise:export')")
    @Log(title = "企业", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Enterprise enterprise)
    {
        List<Enterprise> list = enterpriseService.selectEnterpriseList(enterprise);
        ExcelUtil<Enterprise> util = new ExcelUtil<Enterprise>(Enterprise.class);
        return util.exportExcel(list, "企业数据");
    }

    /**
     * 获取企业详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise:query')")
    @GetMapping(value = "/{enteId}")
    public AjaxResult getInfo(@PathVariable("enteId") Long enteId)
    {
        return AjaxResult.success(enterpriseService.selectEnterpriseById(enteId));
    }

    /**
     * 新增企业
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise:add')")
    @Log(title = "企业", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Enterprise enterprise)
    {
        // 创建日期Date对象
        Date date = new Date();
        // 得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
        Timestamp timeStamp = new Timestamp(date.getTime());
        // 得到创建者帐户ID
        Long createId = SecurityUtils.getLoginUser().getUser().getUserId();
        // 得到创建人帐户用户名
        String createBy = SecurityUtils.getLoginUser().getUser().getUserName();
        // 新增流向来源，设置审核状态为：待提交
        enterprise.setEnteState("initial");
        enterprise.setEnteCreateId(createId);
        enterprise.setEnteCreateTime(timeStamp);
        enterprise.setEnteCreateBy(createBy);
        return toAjax(enterpriseService.insertEnterprise(enterprise));
    }

    /**
     * 修改企业
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise:edit')")
    @Log(title = "企业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Enterprise enterprise)
    {
        // 修改日期Date对象
        Date date = new Date();
        // 得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
        Timestamp timeStamp = new Timestamp(date.getTime());
        // 得到修改者帐户ID
        Long updateId = SecurityUtils.getLoginUser().getUser().getUserId();
        // 得到修改人帐户ID
        String updateBy = SecurityUtils.getLoginUser().getUser().getUserName();
        enterprise.setEnteUpdateId(updateId);
        enterprise.setEnteUpdateTime(timeStamp);
        enterprise.setEnteUpdateBy(updateBy);
        return toAjax(enterpriseService.updateEnterprise(enterprise));
    }

    /**
     * 提交企业供审批
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise:submit')")
    @Log(title = "企业", businessType = BusinessType.UPDATE)
    @DeleteMapping("/submit/{enteIds}")
    public AjaxResult submit(@PathVariable Long[] enteIds)
    {
        // 更新审核状态为“待审核”的条数计数变量
        int result = 1;
        // 创建实例，将状态设置成待审批
        Enterprise enterprise = new Enterprise();
        enterprise.setEnteState("approval");
        for(Long enteId : enteIds){
            enterprise.setEnteId(enteId);
            result = enterpriseService.updateEnterprise(enterprise);
            if(result <= 0)
            {
                return toAjax(result);
            }
        }
        return toAjax(result);
    }

    /**
     * 审批通过企业
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise:pass')")
    @Log(title = "企业", businessType = BusinessType.UPDATE)
    @DeleteMapping("/pass/{enteIds}")
    public AjaxResult pass(@PathVariable Long[] enteIds)
    {
        // 更新审核状态为“待审核”的条数计数变量
        int result = 1;
        // 创建实例，将状态设置成待审批
        Enterprise enterprise = new Enterprise();
        enterprise.setEnteState("definite");
        Long approveId = SecurityUtils.getLoginUser().getUser().getUserId();
        enterprise.setEnteApproveId(approveId);
        String approveBy = SecurityUtils.getLoginUser().getUser().getUserName();
        enterprise.setEnteApproveBy(approveBy);
        // 创建Date对象，并得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
        Date date = new Date();
        Timestamp timeStamp = new Timestamp(date.getTime());
        enterprise.setEnteApproveTime(timeStamp);
        for(Long enteId : enteIds){
            enterprise.setEnteId(enteId);
            result = enterpriseService.updateEnterprise(enterprise);
            if(result <= 0)
            {
                return toAjax(result);
            }
        }
        return toAjax(result);
    }

    /**
     * 反审批退回企业
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise:back')")
    @Log(title = "企业", businessType = BusinessType.UPDATE)
    @DeleteMapping("/back/{enteIds}")
    public AjaxResult back(@PathVariable Long[] enteIds)
    {
        // 更新审核状态为“待审核”的条数计数变量
        int result = 1;
        // 创建实例，将状态设置成待审批
        Enterprise enterprise = new Enterprise();
        enterprise.setEnteState("approvalback");
        Long ApproveId = SecurityUtils.getLoginUser().getUser().getUserId();
        enterprise.setEnteApproveId(ApproveId);
        String approveBy = SecurityUtils.getLoginUser().getUser().getUserName();
        enterprise.setEnteApproveBy(approveBy);
        // 创建Date对象，并得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
        Date date = new Date();
        Timestamp timeStamp = new Timestamp(date.getTime());
        enterprise.setEnteApproveTime(timeStamp);
        for(Long enteId : enteIds){
            enterprise.setEnteId(enteId);
            result = enterpriseService.updateEnterprise(enterprise);
            if(result <= 0)
            {
                return toAjax(result);
            }
        }
        return toAjax(result);
    }

    /**
     * 删除企业
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise:remove')")
    @Log(title = "企业", businessType = BusinessType.DELETE)
	@DeleteMapping("/{enteIds}")
    public AjaxResult remove(@PathVariable Long[] enteIds)
    {
        return toAjax(enterpriseService.deleteEnterpriseByIds(enteIds));
    }
}
